// MainController.java
package com.njwb.Swing.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class MainController {
    private MainFrame mainFrame;
    private String username;
    private int role;

    private List<MainFrame.ScalableComponent> scalableComponents;

    public MainController(MainFrame mainFrame, String username, int role, List<MainFrame.ScalableComponent> scalableComponents){
        this.mainFrame = mainFrame;
        this.username = username;
        this.role = role;
        this.scalableComponents = scalableComponents;

        initButtons();
    }

    private void initButtons(){
        JPanel leftPanel = mainFrame.getLeftPanel();
        JPanel rightPanel = mainFrame.getRightPanel();
        CardLayout cl = (CardLayout) rightPanel.getLayout();

        int buttonWidth = 180, buttonHeight = 40, startY = 10, gapY = 15;
        int y = startY;

        JButton billButton = createImageButton("/com/njwb/Util/images/btn_bill.gif");
        billButton.setBounds(2,y,buttonWidth,buttonHeight);
        leftPanel.add(billButton);
        scalableComponents.add(new MainFrame.ScalableComponent(billButton,2,y,buttonWidth,buttonHeight));
        y += buttonHeight + gapY;

        JButton userButton = null;
        if(role==1){
            userButton = createImageButton("/com/njwb/Util/images/btn_users.gif");
            userButton.setBounds(2,y,buttonWidth,buttonHeight);
            leftPanel.add(userButton);
            scalableComponents.add(new MainFrame.ScalableComponent(userButton,2,y,buttonWidth,buttonHeight));
            y += buttonHeight + gapY;
        }

        JButton supplierButton = createImageButton("/com/njwb/Util/images/btn_suppliers.gif");
        supplierButton.setBounds(2,y,buttonWidth,buttonHeight);
        leftPanel.add(supplierButton);
        scalableComponents.add(new MainFrame.ScalableComponent(supplierButton,2,y,buttonWidth,buttonHeight));
        y += buttonHeight + gapY;

        JButton welcomeButton = createImageButton("/com/njwb/Util/images/btn_reports.gif");
        welcomeButton.setBounds(2,y,buttonWidth,buttonHeight);
        leftPanel.add(welcomeButton);
        scalableComponents.add(new MainFrame.ScalableComponent(welcomeButton,2,y,buttonWidth,buttonHeight));
        y += buttonHeight + gapY;

        JButton exitButton = createImageButton("/com/njwb/Util/images/btn_exit.gif");
        exitButton.setBounds(2,y,buttonWidth,buttonHeight);
        leftPanel.add(exitButton);
        scalableComponents.add(new MainFrame.ScalableComponent(exitButton,2,y,buttonWidth,buttonHeight));

        // 按钮事件
        billButton.addActionListener(e -> cl.show(rightPanel,"BillPanel"));
        if(userButton!=null) userButton.addActionListener(e -> cl.show(rightPanel,"UserPanel"));
        supplierButton.addActionListener(e -> cl.show(rightPanel,"SupplierPanel"));
        welcomeButton.addActionListener(e -> cl.show(rightPanel,"StatisticsPanel"));
        exitButton.addActionListener(e -> mainFrame.exitConfirm());
    }

    private JButton createImageButton(String path){
        ImageIcon icon = new ImageIcon(getClass().getResource(path));
        JButton button = new JButton(icon);
        button.setBorderPainted(false);
        button.setContentAreaFilled(false);
        button.setFocusPainted(false);
        return button;
    }
}
